package ch.bfh.fbi.mobicomp.zap09;

import java.util.concurrent.TimeUnit;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;
import ch.simplix.android.repetitive.service.implementation.ARepetitiveService;
import ch.simplix.android.speech.string.FrenchString;
import ch.simplix.android.speech.string.GermanString;
import ch.simplix.android.speech.string.UKString;

/**
 * Zap08 does allow the CPU to go to deep sleep. However, if the repetition is at about 1 second or even faster, using the AlarmManager is an overkill.
 * 
 * Zap09 uses the ARepetitiveService, which allows to concentrate again on the main algorithm, instead of having to consider all the possibilities
 * within Android to save energy. As one can see, the complexity of the code within Zap09Service drops to a minimum. Great. 
 * 
 * Happy working with the ARepetitiveService. 
 * 
 * @author SimpliX
 *
 */
public class Zap09Service extends ARepetitiveService {

	private static final String TAG = "Zap09Service";	

	private Intent speechIntent = new Intent(
			"ch.simplix.android.speech.SPEECH_SERVICE");
	private long countdown;

	@Override
	public IBinder onBind(Intent intent) {
		// We do not offer explicite binding 
		return null;
	}
	
	@Override
	public void onStart(Intent intent) {
				Log.d(TAG, "destruction initiated...");
				initiateAutoDestruction();
			}
	
	@Override
	public void onStop() {
		Log.d(TAG, "destruction aborted...");
		resetAutoDestruction();		
	}
	@Override
	public void onRepeat() {
		Log.d(TAG, "onRepeat");
		countDown();		
	}
		
	private void resetAutoDestruction() {
		if (countdown == 0)
			return;
		countdown = 0;
		speechIntent.putExtra("Phrase", new UKString(
				"Self destruction sequence aborted."));
		startService(speechIntent);
		speechIntent.putExtra("Phrase", new GermanString(
				"Selbstzerstörungssequenz abgebrochen."));
		startService(speechIntent);
		speechIntent.putExtra("Phrase", new FrenchString(
				"Séquence d'autodestruction avortée."));
		startService(speechIntent);
	}

	private void initiateAutoDestruction() {
		if (countdown != 0)
			return;
		countdown = System.currentTimeMillis() + 3 * 60 * 1000;
		speechIntent.putExtra("Phrase", new UKString(
				"Self destruction sequence initiated."));
		startService(speechIntent);
		speechIntent.putExtra("Phrase", new GermanString(
				"Selbstzerstörungs Sequenz eingeleitet."));
		startService(speechIntent);
		speechIntent.putExtra("Phrase", new FrenchString(
				"Séquence d'autodestruction declanchée."));
		startService(speechIntent);
	}

	private void countDown() {
		int timeLeft = (int) ((countdown - System.currentTimeMillis()) / 1000);
		if (timeLeft > 70) {
			setUpdateInterval(60,TimeUnit.SECONDS);
			minutesCount(timeLeft);
		} else if (timeLeft > 11) {
			setUpdateInterval(10,TimeUnit.SECONDS);
			secondsBlockCount(timeLeft);
		} else if (timeLeft > 0) {
			setUpdateInterval(1,TimeUnit.SECONDS);
			secondsCount(timeLeft);
		} else {
			destroy();
		}
	}

	private void minutesCount(int timeLeft) {
		speechIntent.putExtra("Phrase", new UKString(""
				+ (1 + (timeLeft / (60))) + "Minutes until self destruction"));
		startService(speechIntent);
		speechIntent
				.putExtra("Phrase", new GermanString(""
						+ (1 + (timeLeft / (60)))
						+ "Minuten bis zur Selbstzerstörung"));
		startService(speechIntent);
		speechIntent.putExtra("Phrase", new FrenchString(""
				+ (1 + (timeLeft / (60)))
				+ "minutes jusqu'à ce que l'auto-destruction"));
		startService(speechIntent);
	}

	private void secondsBlockCount(int timeLeft) {
		if (timeLeft % 3 == 0) {
			speechIntent.putExtra("Phrase", new UKString(""
					+ (10 + (timeLeft / 10 * 10))
					+ "Seconds until self destruction"));
		} else if (timeLeft % 3 == 1) {
			speechIntent.putExtra("Phrase", new GermanString(""
					+ (10 + (timeLeft / 10 * 10))
					+ "Sekunden bis zur Selbstzerstörung"));
		} else {
			speechIntent.putExtra("Phrase", new FrenchString(""
					+ (10 + (timeLeft / 10 * 10))
					+ "seconds jusqu'à ce que l'auto-destruction"));
		}
		startService(speechIntent);
	}

	private void secondsCount(int timeLeft) {
		speechIntent.putExtra("Phrase", new UKString("" + (timeLeft)));
		startService(speechIntent);
	}

	private void destroy() {
		countdown = 0;
		super.stopTimedRepetition();
		speechIntent.putExtra("Phrase", new UKString("Self destroying now."));
		startService(speechIntent);
	}

	
	

	
	
}
